package redis

import java.util
import redis.clients.jedis.{HostAndPort, Jedis, JedisCluster, JedisPoolConfig}
/**
  * 静态的连接池
  * 启动集群：redis-cli -c -h nn1.hadoop -p 6379
  */
object JedisClusterConnectionPool {
	val config = new JedisPoolConfig()
	//最大连接数,
	config.setMaxTotal(20)
	//最大空闲连接数
	config.setMaxIdle(10)
	//保持连接活跃
	config.setTestOnBorrow(true)
	
	val jedisClusters = new util.HashSet[HostAndPort]()
	jedisClusters.add(new HostAndPort("nn1.hadoop", 6379))
	jedisClusters.add(new HostAndPort("nn2.hadoop", 6379))
	jedisClusters.add(new HostAndPort("s1.hadoop", 6379))
	jedisClusters.add(new HostAndPort("s2.hadoop", 6379))
	jedisClusters.add(new HostAndPort("s3.hadoop", 6379))
	jedisClusters.add(new HostAndPort("s4.hadoop", 6379))
	val jedisCluster = new JedisCluster(jedisClusters)
	
	def getConnection(): JedisCluster = {
		jedisCluster
	}
	
	def main(args: Array[String]): Unit = {
		//val str = JedisClusterConnectionPool.getConnection().get("hainiu1")
		val conn: JedisCluster = JedisClusterConnectionPool.getConnection()
		conn.set("hainiu1", "1000")
		val r1 = conn.get("hainiu1")
		println(r1)
		conn.incrBy("hainiu1", -50)
		val r2 = conn.get("hainiu1")
		println(r2)
		
		val r = conn.hkeys("*")
		import scala.collection.JavaConversions._
		for (p <- r) {
			println(p + " : " + conn.get(p))
		}
		conn.close()
	}
	
}
//
//import redis.clients.jedis.Jedis
//
//object RedisStringJava {
//	def main(args: Array[String]): Unit = { //连接本地的 Redis 服务
//		val jedis = new Jedis("localhost")
//		System.out.println("连接成功")
//		//设置 redis 字符串数据
//		jedis.set("runoobkey", "www.runoob.com")
//		// 获取存储的数据并输出
//		System.out.println("redis 存储的字符串为: " + jedis.get("runoobkey"))
//	}
//}